package com.liusand.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import com.liusand.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.UUID;

/**
 * @author :liusha
 * packageName :com.liusand.common
 * fileName :commonController
 * creationTime :2022/11/23 15:18
 */
@RestController
@RequestMapping("common")
@Slf4j
public class CommonController {

    @Value("${oss.accessKey}")
    private String accessKey;
    @Value("${oss.secret}")
    private String secret;
    @Value("${oss.endpoint}")
    private String endpoint;
    @Value("${oss.bucketName}")
    private String bucketName;
    @Value("${oss.url}")
    private String url;

    @PostMapping("upload")
    public Result upload(MultipartFile file) {
        if(file == null) {
            return Result.error("上传图片失败");
        }
        String filename = file.getOriginalFilename();
        String newFilename = UUID.randomUUID() + filename.substring(filename.lastIndexOf("."));

        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKey, secret);
        try {
            ossClient.putObject(bucketName, newFilename, new ByteArrayInputStream(file.getBytes()));
        }catch (IOException e){
            return Result.error("上传文件异常");
        }finally {
            ossClient.shutdown();;
        }
//        String newFilename = UUID.randomUUID() + preFilename.substring(preFilename.indexOf("."));
//        try {
//            file.transferTo(new File("D:\\imgs\\" + newFilename));
//        } catch (IOException e) {
//            return Result.error("上传图片失败");
//        }
        return Result.success((Object) (url + "/" + newFilename));
    }

    @GetMapping("download")
    public Result download(String name, HttpServletResponse response) {
        if(!StringUtils.hasText(name)){
            return Result.error("参数异常");
        }

//        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKey, secret);
//        OSSObject ossObject = ossClient.getObject(bucketName, name);
//        BufferedInputStream inputStream = new BufferedInputStream(ossObject.getObjectContent());
        try {
//            BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream("D:\\imgs\\" + name));
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKey, secret);
            OSSObject ossObject = ossClient.getObject(bucketName, name);
            BufferedInputStream inputStream = new BufferedInputStream(ossObject.getObjectContent());
            ServletOutputStream outputStream = response.getOutputStream();
            int cur = 0;
            while((cur = inputStream.read()) != -1){
                outputStream.write(cur);
            }
        }catch (FileNotFoundException e){
            return Result.error("该文件不存在");
        }catch (IOException e){
            return Result.error("文件下载失败");
        }
        return Result.success("OK");
    }
}
